package com.csust.automaticteach.app.rag.config;

import com.alibaba.cloud.ai.dashscope.api.DashScopeApi;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetriever;
import com.alibaba.cloud.ai.dashscope.rag.DashScopeDocumentRetrieverOptions;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.rag.advisor.RetrievalAugmentationAdvisor;
import org.springframework.ai.chat.client.advisor.api.Advisor;
import org.springframework.ai.rag.retrieval.search.DocumentRetriever;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;

/**
 * @ ClassName LearnAppRagCloudAdvisor
 * @ Description rag检索增强自定义配置类
 * @ Author钰玟
 * @ Date2025/6/13 下午4:16
 * @  1.0
 **/
//@Configuration
@Slf4j
public class LearnAppRagCloudAdvisorConfig {

    @Value("${spring.ai.dashscope.api-key}")
    private String dashScopeApiKey;
    private final String KNOWLEDGE_INDEX = "学习资料";

    @Bean
    Advisor LearnAppRagCloudAdvisor() {
        var dashScopeApi =  DashScopeApi.builder()
                .apiKey(System.getenv(dashScopeApiKey))
                .build();
        DocumentRetriever documentRetriever = new DashScopeDocumentRetriever(dashScopeApi,
                DashScopeDocumentRetrieverOptions.builder()
                        .withIndexName(KNOWLEDGE_INDEX)
                        .build());
        return RetrievalAugmentationAdvisor.builder()
                .documentRetriever(documentRetriever)
                .build();

    }
}
